package main.java.com.lee.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2021/12/8
 */
public class RejectedTest {

	public static void main(String[] args) {
		testRejectedPolicy();
	}

	private static void testRejectedPolicy() {
		ThreadPoolExecutor executor = new ThreadPoolExecutor(1,
				3,
				1,
				TimeUnit.SECONDS,
				new LinkedBlockingQueue<>(1),
				Executors.defaultThreadFactory(),
				new MyRejectedExecutionHandler());

		IntStream.rangeClosed(1, 10).forEach(i->{
			executor.submit(() -> {
				try {
					Thread.sleep(1000L);
					System.out.println(i);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		});

		executor.shutdown();
	}
}
